import 'dart:convert';

import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/config/permission_center_config.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/service/sos_warning_service.dart';
import 'package:ls_work/util/request_url_utils.dart';
import 'package:package_info_plus/package_info_plus.dart';

class GlobalConfigs {
  ///点餐显示金额
  static var showPayPrice = true;

  static var useOfflinePatrol = false;

  static var useArea = true;

  static var receiveSos = true;

  static var showSosInStatusBar = false;

  static var batchDealWarning = false;

  static var nfcSubmitDirect = false;

  static bool get submitDirectNfc => nfcSubmitDirect||(RequestUrlUtils.code=="hzflzx"||RequestUrlUtils.code=="zhylycs2");

  static var filterPatrolTask = false;

  static var hasSetWaring = false;

  static var selectWarningType = <int>[];

  static var selectBuildingAndFloor = <SosBuildingData>[];

  static String appVersion = "";

  ///长者筛选-长者列表
  static var elderUseFilter = false;
  ///长者筛选-长者实施
  static var nurseUseFilter = false;
  ///长者筛选-点餐
  static var mealUseFilter = false;

  ///筛选长者
  static List<MedicalElderInfo> savedFilterElder = [];

  ///隐私政策
  static const privacy = "http://47.120.66.147/private.html";
  ///服务协议
  static const agreenment = "http://47.120.66.147/agreement.html";
  ///app权限清单
  static const appPermissionList = "http://47.120.66.147/appPermissionList.html";
  ///第三方共享清单
  static const sdkShare = "http://47.120.66.147/sdkShare.html";
  ///个人信息清单
  static const personalInfoList = "http://47.120.66.147/personalInfoList.html";
  ///隐私政策摘要
  static const privateSimple = "http://47.120.66.147/privateSimple.html";


  static bool hasShowPolicy = false;

  static bool needShowPolicy = false;

  static bool get useNewOrder => true;
  // static bool get useNewOrder =>
  //     RequestUrlUtils.code == "hzflzx" ||
  //     RequestUrlUtils.code == "zhylycs" ||
  //     RequestUrlUtils.code == "zhylycs2" ||
  //     RequestUrlUtils.code == "zhylycslocal" ||
  //     RequestUrlUtils.code == "xcyg";

  static bool get useWeekModel => ["xcyg","zhylycs","zhylycslocal","zhylycs2"].contains(RequestUrlUtils.code);


  ///筛选老人保存的楼栋楼层
  static String filterBuildingId = "";
  static String filterFloorId = "";

  static Future<void> init() async {
    useOfflinePatrol =
        await SpUtil.getBool("useOfflinePatrol", defValue: false)??false;
    // useArea = await SpUtil.getBool("useArea",
    //     defValue: RequestUrlUtils.code == 'xcyg' ? true : false)??false;
    useArea = false;
    // nfcSubmitDirect =  await SpUtil.getBool("nfcSubmitDirect",defValue: RequestUrlUtils.code=='hzflzx'?true:false);
    // filterPatrolTask = await SpUtil.getBool("filterPatrolTask",
    //     defValue: RequestUrlUtils.code == 'hzflzx' ? true : false)??false;
    filterPatrolTask = false;
    var package = await PackageInfo.fromPlatform();
    appVersion = package.version;
    receiveSos = await SpUtil.getBool("receiveSos", defValue: false)??false;
    var defaultShowInBar = RequestUrlUtils.code=="qaxhy"||RequestUrlUtils.code=="xhycs";
    showSosInStatusBar = await SpUtil.getBool("showSosInStatusBar", defValue: defaultShowInBar)??defaultShowInBar;
    var defaultBatch = RequestUrlUtils.code=="qaxhy"||RequestUrlUtils.code=="xhycs";
    batchDealWarning = await SpUtil.getBool("batchDealWarning", defValue: defaultBatch)??defaultBatch;
    hasShowPolicy =  SpUtil.getBool("hasShowPolicy",defValue: false)??false;
    filterFloorId = SpUtil.getString("filterFloorId",defValue: "")??"";
    filterBuildingId = SpUtil.getString("filterBuildingId",defValue: "")??"";
    await getWarningInfo();
    getSavedOld();
    PermissionCenterConfig.initPermissionConfig();
  }

  static Future<void> saveShowPolicy(bool hasShow) async {
    hasShowPolicy = hasShow;
    await SpUtil.putBool("hasShowPolicy", hasShowPolicy);
  }

  static Future<void> saveUseOfflineSetting(bool use) async {
    useOfflinePatrol = use;
    await SpUtil.putBool("useOfflinePatrol", use);
    eventBus.fire(SendMessageEvent(MessageConfig.REFRESH_LOCAL_PATROL));
  }

  static Future<void> saveBuildingId(String buildingId) async {
    filterBuildingId = buildingId;
    await SpUtil.putString("filterBuildingId", buildingId);
  }

  static Future<void> saveFloorId(String floorId) async {
    filterFloorId = floorId;
    await SpUtil.putString("filterFloorId", floorId);
  }

  static Future<void> saveUseArea(bool use) async {
    useArea = use;
    await SpUtil.putBool("useArea", use);
  }

  static Future<void> saveShowSosInStatusBar(bool show) async {
    showSosInStatusBar = show;
    await SpUtil.putBool("showSosInStatusBar", show);
  }

  // static Future<void> saveNfcSubmitDirect(bool enable) async {
  // nfcSubmitDirect = enable;
  // await SpUtil.putBool("nfcSubmitDirect", enable);
  // }

  static Future<void> saveFilterPatrolTask(bool use) async {
    filterPatrolTask = use;
    await SpUtil.putBool("filterPatrolTask", use);
  }

  static Future<void> saveReceiveSos(bool save) async {
    receiveSos = save;
    await SpUtil.putBool("receiveSos", save);
    SosWarningService.instance.sendEnableReceive(save);
  }

  static Future<void> saveBatchWarning(bool save) async {
    batchDealWarning = save;
    await SpUtil.putBool("batchDealWarning", save);
  }

  ///获取报警设置
   static Future<void> getWarningInfo() async {
    var typeStr =  await SpUtil.getString("sosTypeIds");
    var buildingStr = await SpUtil.getString("sosBuildings");
    hasSetWaring = await SpUtil.getBool("hasSetWaring",defValue: false)??false;

    if(!typeStr.isNullOrEmpty()){
      selectWarningType =  ((jsonDecode(typeStr!) as List?)??[]).map((e) => e as int).toList();
    }
    if(!buildingStr.isNullOrEmpty()){
      selectBuildingAndFloor =  ((jsonDecode(buildingStr!) as List?)??[]).map((e) => SosBuildingData.fromMap(e)).toList();
    }

    SosWarningService.instance.setReceiveArea(GlobalConfigs.selectWarningType,GlobalConfigs.selectBuildingAndFloor);

   }

  ///保存报警设置
  static Future<void> saveWarningInfo(List<num> typeIds,List<SosBuildingData> buildings) async {
    // selectWarningType = typeIds;
    // selectBuildingAndFloor = buildings;
    await SpUtil.putBool("hasSetWaring", true);
    await SpUtil.putString("sosTypeIds", jsonEncode(typeIds));
    await SpUtil.putString("sosBuildings", jsonEncode(buildings.map((e) => e.toMap()).toList()));
    await getWarningInfo();
    // SosWarningService.instance.setReceiveArea(GlobalConfigs.selectWarningType,GlobalConfigs.selectBuildingAndFloor);
  }

  static Future<void> saveOldFilter({bool elderUseFilter = false,bool nurseUseFilter = false,bool mealUseFilter = false,List<MedicalElderInfo>? filterOld}) async {
    GlobalConfigs.elderUseFilter = elderUseFilter;
    GlobalConfigs.nurseUseFilter = nurseUseFilter;
    GlobalConfigs.mealUseFilter = mealUseFilter;
    savedFilterElder = filterOld??[];
    await SpUtil.putBool("elderUseFilter", elderUseFilter);
    await SpUtil.putBool("nurseUseFilter", nurseUseFilter);
    await SpUtil.putBool("mealUseFilter", mealUseFilter);
    var json = savedFilterElder.map((e)=>e.toMap()).toList();
    await SpUtil.putString("savedElder", jsonEncode(json));
  }

  static void getSavedOld(){
    elderUseFilter = SpUtil.getBool("elderUseFilter",defValue: false)??false;
    nurseUseFilter = SpUtil.getBool("nurseUseFilter",defValue: false)??false;
    mealUseFilter = SpUtil.getBool("mealUseFilter",defValue: false)??false;
    var jsonStr = SpUtil.getString("savedElder")??"";
    if(!jsonStr.isNullOrEmpty()){
      var json = jsonDecode(jsonStr) as List;
      savedFilterElder = json.map((e)=>MedicalElderInfo.fromJson(e)).toList();
    }
  }

  static String get splashImage{
    var map = <String,String?>{
      "szylhly":"start_page_gf",
      "gfszky": "start_page_gf"
    };
    return map[RequestUrlUtils.code]??"start_page_common";
  }

  static String? get loginLogo{
    var map = <String,String?>{
      "szylhly":"login_logo",
      "xcyygy":"xc_login_logo"
      // "gfszky": "login_logo"
    };
    return map[RequestUrlUtils.code];
  }

  static String? get homeLogo{
    var map = <String,String?>{
      "szylhly":"icon_home_logo",
      "xcyygy":"icon_home_logo_xc",
      // "gfszky": "icon_home_logo"
    };
    return map[RequestUrlUtils.code];
  }
}

class SosBuildingData{
  dynamic buildingId;
  List<dynamic>? floorIds;

  SosBuildingData({this.buildingId, this.floorIds});

  Map<String, dynamic> toMap() {
    return {
      'buildingId': this.buildingId,
      'floorIds': this.floorIds,
    };
  }

  factory SosBuildingData.fromMap(Map<String, dynamic> map) {
    return SosBuildingData(
      buildingId: map['buildingId'] ,
      floorIds: map['floorIds'],
    );
  }
}
